package com.huiquan.analysis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huiquan.analysis.dao.AnalysisVocabularyTempDao;
import com.huiquan.analysis.domain.AnalysisVocabularyTemp1;
import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;

@Service
public class AnalysisRuleExecuteService extends BaseService {

    @Autowired
    private AnalysisVocabularyTempDao analysisVocabularyTempDao;

    /**
     * 根据rule执行修改Sentence和Vocabulary
     *
     * @param id         规则的id
     * @param rule       拆分或合并规则
     * @param base       被修改的词语
     * @param ruleType   1是拆分，2是合并
     * @param type       语句类型：1是主诉，2是现病史/既往史，3是医学影像
     * @param secondType 现病史二级分类
     */
    public void execute(Long id, String rule, String base, int ruleType, int type, User user, String secondType) {
        if (ruleType == 1) {
            // 拆分类型
            excuteSplit(rule, base, type, user.getUserId(), secondType);
        } else if (ruleType == 2) {
            // 合并类型
            excuteMerge(id, type, secondType);
        } else if (ruleType == 3) {
            // 短语修改属性类型
            excuteUpdate(rule, base, type, user.getUserId(), secondType);
        } else if (ruleType == 4) {
            // 多对多分拆型，先合并，再拆分
            excuteMerge(id, type, secondType);
            excuteSplit(rule, base, type, user.getUserId(), secondType);
        }
    }

    private void excuteMerge(Long id, int type, String secondType) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("type", type);
        param.put("secondType", secondType);
        analysisVocabularyTempDao.excuteMerge(param);
    }

    private void excuteUpdate(String rule, String base, int type, Long userId, String secondType) {
        String[] rDetails = rule.replace(";", "").split("_", 3);

        Map<String, Object> param = new HashMap<>();
        param.put("vocabulary", rDetails[0]);
        param.put("characteristic", rDetails[1]);
        param.put("property", rDetails[2]);

        String[] bDetails = base.split("_", 3);
        if (bDetails.length > 1) {
            String oldCharacteristic = bDetails[1].trim();
            String oldProperty = bDetails[2].trim();
            if (!oldCharacteristic.isEmpty()) {
                param.put("oldCharacteristic", oldCharacteristic);
            }
            if (!oldProperty.isEmpty()) {
                param.put("oldProperty", oldProperty);
            }
        }
        param.put("type", type);
        param.put("userId", userId);
        param.put("secondType", secondType);
        analysisVocabularyTempDao.excuteUpdate(param);
    }

    private void excuteSplit(String rule, String base, int type, Long userId, String secondType) {
        // 拆分rule，获取明细
        String[] rRuleArray = rule.split(";");
        String[] ruleDetails;
        StringBuilder change = new StringBuilder();
        change.append((char) 1);
        List<AnalysisVocabularyTemp1> vocabularyTemp1s = new ArrayList<>();
        for (int i = 0; i < rRuleArray.length; i++) {
            String rRuleStr = rRuleArray[i];
            ruleDetails = rRuleStr.split("_", 3);
            AnalysisVocabularyTemp1 vocabularyTemp1 = new AnalysisVocabularyTemp1();
            vocabularyTemp1.setSequence(i + 1);
            vocabularyTemp1.setBase(base);
            vocabularyTemp1.setVocabulary(ruleDetails[0]);
            vocabularyTemp1.setCharacteristic(ruleDetails[1]);
            vocabularyTemp1.setProperty(ruleDetails[2]);
            vocabularyTemp1s.add(vocabularyTemp1);
            change.append(ruleDetails[0]);
            change.append((char) 1);
        }
        // 获取temp1表的数据放入map中
        Map<String, Object> param = new HashMap<>();
        param.put("temp1s", vocabularyTemp1s);
        param.put("change", change.toString());
        param.put("base", base);
        param.put("type", type);
        param.put("userId", userId);
        param.put("secondType", secondType);
        analysisVocabularyTempDao.excuteSplit(param);
    }
}
